package com.nj.interfaces.user.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.nj.forward.user.login.vo.UserLoginVo;
import com.nj.framework.util.SpringMVCResult;
import com.nj.interfaces.login.service.UserService;

@Controller
public class UserAction {

	@Autowired
	private UserService userLoginService;

	/**
	 * 方法名： getUserInfo 描述： 获取用户信息接口
	 * 
	 * @author 吴志虎
	 * @param request
	 * @param response
	 * @return
	 * 
	 */
	@RequestMapping("/getUserInfo.action")
	public ResponseEntity<String> getUserInfo(HttpServletRequest request, HttpServletResponse response) {
		String userCode = request.getParameter("userName"); // 用户code
		UserLoginVo returnVo = new UserLoginVo();
		try {
			Map<String, String> loginResult = userLoginService.getUserInfo(userCode);
			if ("Y".equals(MapUtils.getString(loginResult, "flag"))) {
				// 成功
				returnVo.setFlag("1");
				// logger.info();
			} else {
				// 失败
				returnVo.setFlag("0");
				returnVo.setFailReason(MapUtils.getString(loginResult, "errorMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnVo.setFlag("0");
			returnVo.setFailReason("系统错误");
		}
		return SpringMVCResult.returnResponseEntity(JSON.toJSONString(returnVo));
	}

	/**
	 * 方法名： getUserCourse 描述： 获取课程表信息
	 * 
	 * @author 吴志虎
	 * @param request
	 * @param response
	 * @return
	 * 
	 */
	@RequestMapping("/getUserCourse.action")
	public ResponseEntity<String> getUserCourse(HttpServletRequest request, HttpServletResponse response) {
		String userCode = request.getParameter("userName"); // 用户code
		UserLoginVo returnVo = new UserLoginVo();
		try {
			Map<String, String> loginResult = userLoginService.getUserInfo(userCode);
			if ("Y".equals(MapUtils.getString(loginResult, "flag"))) {
				// 成功
				returnVo.setFlag("1");
				// logger.info();
			} else {
				// 失败
				returnVo.setFlag("0");
				returnVo.setFailReason(MapUtils.getString(loginResult, "errorMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnVo.setFlag("0");
			returnVo.setFailReason("系统错误");
		}
		return SpringMVCResult.returnResponseEntity(JSON.toJSONString(returnVo));
	}

	/**
	 * 方法名： getUserCondition 描述： 获取用户状况
	 * 
	 * @author 吴志虎
	 * @param request
	 * @param response
	 * @return
	 * 
	 */
	@RequestMapping("/getUserCondition.action")
	public ResponseEntity<String> getUserCondition(HttpServletRequest request, HttpServletResponse response) {
		String userCode = request.getParameter("userName"); // 用户code
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> userCondition = userLoginService.getUserCondition(userCode);
			map.put("userCondition", userCondition);
			map.put("flag", "1");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", "10");
		}
		return SpringMVCResult.returnResponseEntity(JSON.toJSONString(map));
	}

	/**
	 * 方法名： leaveApply 描述： 请假接口
	 * 
	 * @author 吴志虎
	 * @param request
	 * @param response
	 * @return
	 * 
	 */
	@RequestMapping("/leaveApply.action")
	public ResponseEntity<String> leaveApply(HttpServletRequest request, HttpServletResponse response) {
		String userCode = request.getParameter("userCode"); // 用户code
		String courseId = request.getParameter("courseId"); // 课程号
		String leaveType = request.getParameter("leaveType"); // 请假类型(1按天请假2请课假3销假 )
		String weekDay = request.getParameter("weekDay"); // 请假星期 （1,2,3,4,5）
		String courseNumber = request.getParameter("courseNumber"); // 请假课 （1,2,3,4,5,6,7,8,9,10,11,12）

		UserLoginVo returnVo = new UserLoginVo();
		try {
			Map<String, String> loginResult = userLoginService.getUserInfo(userCode);
			if ("Y".equals(MapUtils.getString(loginResult, "flag"))) {
				// 成功
				returnVo.setFlag("1");
				// logger.info();
			} else {
				// 失败
				returnVo.setFlag("0");
				returnVo.setFailReason(MapUtils.getString(loginResult, "errorMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnVo.setFlag("0");
			returnVo.setFailReason("系统错误");
		}
		return SpringMVCResult.returnResponseEntity(JSON.toJSONString(returnVo));
	}

}
